/**
 * Filename: AlbumWallAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/8 15:20
 */
package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.MainActivity;
import com.github.postsanf.yinian.activity.YunAlbumActivity;
import com.github.postsanf.yinian.activity.YunAlbumActivity2;
import com.github.postsanf.yinian.bean.RepaceImg;
import com.github.postsanf.yinian.bean.YNPhoto;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.utils.UIHelper;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.StringUtils;
import com.github.postsanf.yinian.utils.YNUrlHelper;

import java.util.ArrayList;
import java.util.List;

public class YunAlbumWallAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Activity activity;
    private Context context;
    private List<YNPhoto> datas;
    private List<RepaceImg> repaceImgs = new ArrayList<>();

    public YunAlbumWallAdapter(Context context, List<YNPhoto> datas, Activity activity) {
        this.context = context;
        this.datas = datas;
        this.activity = activity;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout ll_item_album_wall;                                    //列表View
        SimpleDraweeView iv_album_grid_item_image;                            //照片墙图片
        ImageView mSelect;

        public ViewHolder(final View itemView) {
            super(itemView);
            ll_item_album_wall = (RelativeLayout) itemView
                    .findViewById(R.id.iv_album_grid_item);
            iv_album_grid_item_image = (SimpleDraweeView) itemView
                    .findViewById(R.id.iv_album_grid_item_image);
            mSelect = (ImageView) itemView
                    .findViewById(R.id.id_item_select);
            itemView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    ViewGroup.LayoutParams vlp = itemView.getLayoutParams();
                    vlp.height = itemView.getMeasuredWidth();
                    vlp.width = itemView.getMeasuredWidth();
                    itemView.setLayoutParams(vlp);
                    return true;
                }
            });
        }

        public void bindData(final YNPhoto listItems, final int position) {
            if (listItems != null) {
//                if (repaceImgs.size() > 0) {
//                    for (int i = 0; i < repaceImgs.size(); i++) {
//                        RepaceImg repaceImg = repaceImgs.get(i);
//                        if (repaceImg.getPosition() == position && repaceImg.isScale()) {
//                            mSelect.setImageResource(R.drawable.pictures_selected);
//                            //  iv_album_grid_item_image.setColorFilter(Color.parseColor("#33000000"));
//                        } else {
//                            mSelect.setImageResource(R.drawable.picture_unselected);
//                            // iv_album_grid_item_image.setColorFilter(null);
//                        }
//                    }
//                }
                final String item = listItems.getUrl();
                if(SelectedImg.mSelectedImage.contains(item)){
                    mSelect.setImageResource(R.drawable.pictures_selected);
                }else{
                    mSelect.setImageResource(R.drawable.picture_unselected);
                }
                String paths = YNUrlHelper.getWallUrl(context, listItems.getThumbnail());
                Uri uri = Uri.parse(StringUtils.repaceImgUrl(paths));
                ImageOptHelper.showFrescoImg(uri, iv_album_grid_item_image, context);
                iv_album_grid_item_image.setOnClickListener(new View.OnClickListener() {
                    //选择，则将图片变暗，反之则反之
                    @Override
                    public void onClick(View v) {
                        if (SelectedImg.mSelectedImage.size() < UIHelper.MIN_IMG_COUNT) {
                            if (SelectedImg.mSelectedImage.contains(item)) {
                                // 已经选择过该图片
                                for (int i = 0; i < repaceImgs.size(); i++) {
                                    RepaceImg repaceImg = repaceImgs.get(i);
                                    if (repaceImg.getPosition() == position) {
                                        repaceImg.setScale(false);
                                    }
                                }
                                SelectedImg.mSelectedImage.remove(item);
                                mSelect.setImageResource(R.drawable.picture_unselected);
                                iv_album_grid_item_image.setColorFilter(null);
                            } else {
                                // 未选择该图片
                                RepaceImg repaceImg = new RepaceImg();
                                repaceImg.setPosition(position);
                                repaceImg.setScale(true);
                                repaceImgs.add(repaceImg);
                                SelectedImg.mSelectedImage.add(item);
                                mSelect.setImageResource(R.drawable.pictures_selected);
                                iv_album_grid_item_image.setColorFilter(Color.parseColor("#33000000"));
                            }
                        } else if (SelectedImg.mSelectedImage.size() >= UIHelper.MIN_IMG_COUNT) {
                            if (SelectedImg.mSelectedImage.contains(item)) {
                                SelectedImg.mSelectedImage.remove(item);
                                mSelect.setImageResource(R.drawable.picture_unselected);
                                iv_album_grid_item_image.setColorFilter(null);
                            }
                        }
                        if (MainActivity.goodsid==1||MainActivity.goodsid==99){
                            ((YunAlbumActivity) activity).setSureCount(SelectedImg.mSelectedImage.size());
                        }else if(MainActivity.goodsid==2){
                            ((YunAlbumActivity) activity).setSureCount(SelectedImg.mSelectedImage.size());
                        }else if(MainActivity.goodsid==3||MainActivity.goodsid==4){
                            ((YunAlbumActivity2) activity).setSureCount(SelectedImg.mSelectedImage.size());
                        }
                    }
                });
            }
        }
    }

    private YNPhoto getItem(int position) {
        return datas.get(position);
    }

    // 创建ViewHolder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_yunalbum_select_item, parent, false);
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder rv_holder, int position) {
        if (rv_holder instanceof ViewHolder) {
            final ViewHolder holder = (ViewHolder) rv_holder;
            YNPhoto listItems = getItem(position);
            holder.bindData(listItems, position);
            holder.itemView.setTag(listItems);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public int getIdByDirection(boolean isTop) {
        if (isTop) {
            if (datas.size() <= 0) {
                return 0;
            }
            return datas.get(0).getPid();
        } else {
            return datas.get(datas.size() - 1).getPid();
        }
    }

    public void addItemsFromBottom(List<YNPhoto> items) {
        if (items == null)
            return;
        this.datas.addAll(datas.size(), items);
        this.notifyItemRangeInserted(datas.size(), items.size());
    }

    public void addItemsFromTop(List<YNPhoto> items) {
        if (items == null)
            return;
        this.datas.addAll(0, items);
        this.notifyItemRangeInserted(0, items.size());
    }
}
